package com.app.config;

import com.app.pojo.Student;
import com.app.pojo.Teacher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Student student= (Student) session.getAttribute("student");
        Teacher teacher= (Teacher) session.getAttribute("teacher");
        if(request.getServletPath().indexOf("teacher")>-1){
            if(teacher==null){
                response.sendRedirect(request.getContextPath()+"/teacher/login?error="+java.net.URLEncoder.encode("请先登录!!!","utf-8"));
                return  false;
            }
        }else if(request.getServletPath().indexOf("student")>-1){
          if(student==null){
              response.sendRedirect(request.getContextPath()+"/student/login?error="+java.net.URLEncoder.encode("请先登录!!!","utf-8"));
              return  false;
          }
      }
        return true;
    }
}
